import hashlib
import os
import requests
import uuid


def get_file_md5(path: str,
                 save_path: str = "") -> (bool, str):
    # 判断文件夹是否存在
    if not os.path.exists(save_path):
        os.mkdir(save_path)
    save_file_name = f"{uuid.uuid4().hex}.apk"
    save_path = os.path.join(save_path, save_file_name)
    res = requests.get(path, stream=True, verify=False)
    with open(save_path, "wb") as file:
        for ch in res.iter_content(chunk_size=1024):
            file.write(ch)
        return True, save_path


def md5(path: str):
    hash_md5 = hashlib.md5()
    with open(path, "rb") as f:
        for chunk in iter(lambda: f.read(4096), b""):
            hash_md5.update(chunk)
    return hash_md5.hexdigest()


def download(path: str, save_dir: str, save_file_name) -> (bool, str):
    save_file = os.path.join(save_dir, save_file_name)
    # 判断文件夹是否存在
    if not os.path.exists(save_dir):
        os.mkdir(save_dir)

    res = requests.get(path, stream=True, verify=False)
    with open(save_file, "wb") as file:
        for ch in res.iter_content(chunk_size=1024):
            file.write(ch)
        return True, save_file
